package com.interagriculture.controller;

import com.interagriculture.annotation.AdminVerify;
import com.interagriculture.common.CustomException;
import com.interagriculture.common.Result;
import com.interagriculture.entity.Achievement;
import com.interagriculture.enums.ExceptionCodeEnum;
import com.interagriculture.service.AchievementService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * @author 11 2024/11/12
 */
@RestController
@RequestMapping("/adminAchievement")
public class AdminAchievementController {

    @Resource
    private AchievementService achievementService;

    @PostMapping("/saveOrUpdateInfo")
    @AdminVerify
    public Result saveOrUpdateInfo(@RequestBody Achievement achievement) {
        achievementService.saveOrUpdateInfo(achievement);
        return Result.ok();
    }
    @GetMapping("/{id}")
    @AdminVerify
    public Result<Achievement> getById(@PathVariable @NotNull Integer id) {
        Achievement achievement = achievementService.queryById(id);
        return Result.ok(achievement);
    }
    @PostMapping("/delete")
    @AdminVerify
    public Result deleteById(@RequestParam @NotNull Integer id) {
        boolean deleteRes = achievementService.deleteById(id);
        if(!deleteRes){
            throw new CustomException(ExceptionCodeEnum.DELETE_ERROR);
        }
        return Result.ok();
    }

}
